#include "../include/head.h"
//创建一个链表 带头节点
Pnode_list creat_list()
{
    Pnode_list head=(Pnode_list)malloc(sizeof(Node_list));
    if(head==NULL)
    {
       printf("malloc failed\n");
       return NULL;
    }
    // head->info.score;
    //memset(head,0,sizeof(Node_list));
    head->front=head->next=head;//
    return head;
}
//尾插
int tail_insert(Pnode_list head,Data_type value)
{
    //判空  --省略了
    if(head==NULL){printf("LIST IS ERROR\n");return -1;};
    //创建一个新的结点
    Pnode_list newnode=(Pnode_list)malloc(sizeof(Node_list));
    if(newnode==NULL)
    {
       printf("malloc failed\n");
       return -1;
    }
    memcpy(&newnode->info,&value,sizeof(Data_type));
    newnode->front=newnode->next=NULL;
    
    //找到尾结点
    newnode->next = head;
    newnode->front = head->front;
    head->front->next = newnode;
    head->front = newnode;
    // Pnode_list tailnode=head->front;
   
    // tailnode->next=newnode;
   
    // newnode->front=tailnode;
  
    // newnode->next=head;
    // head->front=newnode;
    return 0;
}

void clear_list(Pnode_list head)
 {
    if (head == NULL) {
        fprintf(stderr, "错误：传入空指针\n");
        return;
    }

    Pnode_list p = head->next;  // 从第一个有效节点开始
    while (p != head) {         // 循环链表判断
        Pnode_list temp = p;    // 保存当前节点
        p = p->next;            // 移动到下一个节点
        
        // 安全释放节点
        temp->front = NULL;      // 断开前驱指针
        temp->next = NULL;
        free(temp);             // 释放内存
        temp = NULL;            // 避免悬垂指针
    }

    // 重置头节点指针
    head->next = head;
    head->front = head;
    
    // curman = 0;  // 更新全局计数器
    printf("链表已清空\n");
}







Pnode_student creat_list_stu()
{
    Pnode_student head=(Pnode_student)malloc(sizeof(StudentNode));
    if(head==NULL)
    {
       printf("malloc failed\n");
       return NULL;
    }
    // head->info.sub;
    //memset(head,0,sizeof(node));
    head->front=head->next=head;//
    return head;
}
//尾插
int tail_insert_stu(Pnode_student head,Data_type value)
{
    Pnode_student new_node = (Pnode_student)malloc(sizeof(StudentNode));
    if (!new_node) {
        printf("内存分配失败\n");
        return 0;
    }
    
    // 确保所有字段都被复制
    strcpy(new_node->info.id, value.id);
    strcpy(new_node->info.name, value.name);
    strcpy(new_node->info.class, value.class);
    for (int i = 0; i < 6; i++) {
        new_node->info.score[i] = value.score[i];
    }
    
    // 插入链表
    new_node->front = head->front;
    new_node->next = head;
    head->front->next = new_node;
    head->front = new_node;
    return 0;
}

void clear_list_stu(Pnode_student head)
 {
    if (head == NULL) {
        fprintf(stderr, "错误：传入空指针\n");
        return;
    }

    Pnode_student p = head->next;  // 从第一个有效节点开始
    while (p != head) {         // 循环链表判断
        Pnode_student temp = p;    // 保存当前节点
        p = p->next;            // 移动到下一个节点
        
        // 安全释放节点
        temp->front = NULL;      // 断开前驱指针
        temp->next = NULL;
        free(temp);             // 释放内存
        temp = NULL;            // 避免悬垂指针
    }

    // 重置头节点指针
    head->next = head;
    head->front = head;
    
    // curman = 0;  // 更新全局计数器
    printf("链表已清空\n");
}





Pnode_teacher creat_list_tea()
{
    Pnode_teacher head=(Pnode_teacher)malloc(sizeof(TeacherNode));
    if(head==NULL)
    {
       printf("malloc failed\n");
       return NULL;
    }
    // head->info.sub;
    //memset(head,0,sizeof(node));
    head->front=head->next=head;//
    return head;
}
//尾插
int tail_insert_tea(Pnode_teacher head,data_type value)
{
    //判空  --省略了
    if(head==NULL){printf("LIST IS ERROR\n");return -1;};
    //创建一个新的结点
    Pnode_teacher newnode=(Pnode_teacher)malloc(sizeof(TeacherNode));
    if(newnode==NULL)
    {
       printf("malloc failed\n");
       return -1;
    }
    memcpy(&newnode->data,&value,sizeof(data_type));
    newnode->front=newnode->next=NULL;
    
    //找到尾结点
    newnode->next = head;
    newnode->front = head->front;
    head->front->next = newnode;
    head->front = newnode;
    // Pnode_teacher tailnode=head->front;
   
    // tailnode->next=newnode;
   
    // newnode->front=tailnode;
  
    // newnode->next=head;
    // head->front=newnode;
    return 0;
}

void clear_list_tea(Pnode_teacher head)
 {
    if (head == NULL) {
        fprintf(stderr, "错误：传入空指针\n");
        return;
    }

    Pnode_teacher p = head->next;  // 从第一个有效节点开始
    while (p != head) {         // 循环链表判断
        Pnode_teacher temp = p;    // 保存当前节点
        p = p->next;            // 移动到下一个节点
        
        // 安全释放节点
        temp->front = NULL;      // 断开前驱指针
        temp->next = NULL;
        free(temp);             // 释放内存
        temp = NULL;            // 避免悬垂指针
    }

    // 重置头节点指针
    head->next = head;
    head->front = head;
    
    // curman = 0;  // 更新全局计数器
    printf("链表已清空\n");
}